
import requests
import json
import time
from tqdm import tqdm # 导入 tqdm

# --- 配置信息 ---
BASE_URL = "http://69.231.135.101/pgktstudent/messageJW/send"
HEADERS = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Authorization": "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjczYWUwNDMzLTAyY2EtNGViYS1iMzMwLTAyMDNmZDA4NjkwZiJ9.NROPhEwieaFuvyFzdvEyfwGArmm1Qd6sSsHRVzDXJZGyo1UhZzhvGb7HN_bp26uLZfm1zfQ4S9OwQjJ7JnRpqA",  # 替换为新的 Authorization Token
    # 替换为你的有效Authorization Token
    "Content-Type": "application/json",
    "Origin": "http://69.231.135.101",
    "Proxy-Connection": "keep-alive",
    "Referer": "http://69.231.135.101/pgktjwsnew/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0",
}

# 视频的固定信息 (从你的请求中提取)
VIDEO_INFO = {
    "key": "Kci5Fx2nUH3LcInM31UDSEhaqAQMndqvCsWHyeP/j/cVDfuamQ+2p3KR4l/Z1YG59C3eLtER2ZPMfP+Nsh2l4GDCW0Hci1L5V1WEJYpfWCJelU/1XBhNIPrVGylzcyFMe1+l+MgBElzIGe7HvJdY2VC1UXG3oTixZ7jpFloZyYk6j1VC10NZUr1twb1z0jDl1OeC9UScYv+VeGl2YKPzDQ==",  # 替换为新的 key
    "id": "0f4f0a742b974663944d3be692e38628",
}

# --- 模拟视频参数 ---
TOTAL_VIDEO_DURATION_SECONDS = 1872.8  # 视频总时长 (根据你之前日志中的 'pu' 字段的 playPosition 估算)
REPORT_INTERVAL_SECONDS = 120  # 每隔多少秒上报一次进度 (例如，每次上报进度增加120秒)
FLOAT_PRECISION = 6  # 浮点数精度，用于 playPosition 和 playRatio

# --- 循环控制参数 ---
NUM_SIMULATION_LOOPS = 1000 # 你希望模拟观看的次数


# --- 辅助函数 ---
def send_progress_request(position, ratio, status):
    """
    发送一个播放进度上报请求
    """
    data = {
        **VIDEO_INFO,  # 合并视频固定信息
        "playPosition": round(position, FLOAT_PRECISION),
        "playRatio": round(ratio, FLOAT_PRECISION),
        "playStatus": status
    }

    try:
        # verify=False 对应 curl 的 --insecure 选项，跳过SSL验证
        response = requests.post(BASE_URL, headers=HEADERS, data=json.dumps(data), verify=False)
        response.raise_for_status()  # 如果状态码不是2xx，则抛出HTTPError
    except requests.exceptions.HTTPError as errh:
        print(f"HTTP Error: {errh}")
    except requests.exceptions.ConnectionError as errc:
        print(f"Error Connecting: {errc}")
    except requests.exceptions.Timeout as errt:
        print(f"Timeout Error: {errt}")
    except requests.exceptions.RequestException as err:
        print(f"Something went wrong: {err}")


# --- 主模拟逻辑 ---
def simulate_single_watch_session():
    """
    模拟一次完整的视频观看会话（从开始到结束）。
    """
    current_position = 0.0

    # 1. 发送初始状态：waiting (playPosition=0)
    send_progress_request(0, 0, "waiting")

    # 2. 逐步发送 playing 状态的请求
    while current_position < TOTAL_VIDEO_DURATION_SECONDS:
        current_position += REPORT_INTERVAL_SECONDS

        # 确保不会超出视频总时长
        if current_position > TOTAL_VIDEO_DURATION_SECONDS:
            current_position = TOTAL_VIDEO_DURATION_SECONDS

        current_ratio = current_position / TOTAL_VIDEO_DURATION_SECONDS

        send_progress_request(current_position, current_ratio, "playing")

        # 如果达到了视频结尾，就停止生成
        if current_position == TOTAL_VIDEO_DURATION_SECONDS:
            break

    # 3. 发送结束状态 (可选，但推荐)
    send_progress_request(TOTAL_VIDEO_DURATION_SECONDS, 1.0, "ended")


def main():
    """
    主函数，控制循环模拟观看。
    """
    print(f"准备开始模拟视频观看，共循环 {NUM_SIMULATION_LOOPS} 次。")
    print(f"每个视频会话总时长: {TOTAL_VIDEO_DURATION_SECONDS} 秒，每 {REPORT_INTERVAL_SECONDS} 秒上报一次进度。")
    print("---")

    # 仅使用 tqdm 包装外部循环，显示整体进度、已用时间和预计时间
    for i in tqdm(range(1, NUM_SIMULATION_LOOPS + 1), desc="整体进度", unit="会话"):
        simulate_single_watch_session()

        # 如果不是最后一次循环，可以在每次循环结束后稍作等待，模拟用户在不同时间观看
        if i < NUM_SIMULATION_LOOPS:
            time.sleep(1)  # 可以在这里添加一个间隔，模拟用户在不同时间观看

    print("\n所有视频观看模拟已完成。")


if __name__ == "__main__":
    main()